#include "winwidget.h"

#include <QLabel>
#include <QPushButton>

#include "control.h"
#include "snakegame.h"

IconWidget::IconWidget(QImage ic, QWidget *par)
    : icon(ic), QGLWidget(par)
{ ; }

void IconWidget::setIconImage(QImage ic)
{
    icon = ic;
}

void IconWidget::paintGL()
{
    QPainter painter(this);
    painter.drawImage(QRect(0,0,width(), height()), icon);
    painter.end();
}

WinWidget::WinWidget(QWidget *parent) :
    QWidget(parent)
{ ; }

AboutWindow::AboutWindow(Control* c, QWidget *par)
    : control(c), WinWidget(par)
{
    resize(390, 275);
    setWindowTitle(tr("About Snake Game"));
    IconWidget* img = new IconWidget( QImage("src/icons/qt-icon.png"), this);
    img->setGeometry(10, 10, 60, 60);
    QFont font;
    font.setBold(true);
    font.setItalic(true);
    font.setPointSize(22);
    QLabel* labelTittle = new QLabel(tr("Snake Game"),this);
    labelTittle->setGeometry(130, 15, 300, 25);
    labelTittle->setFont(font);
    font.setBold(false);
    font.setPointSize(13);
    QLabel* labelMsg = new QLabel(tr("Free for Fun"),this);
    labelMsg->setGeometry(280, 40, 100, 25);
    labelMsg->setFont(font);
    QLabel* labelTxt = new QLabel(tr("This game was developed by Leandro Cruz.\n"
                                     "contact: lruz@impa.br\n\n"
                                     "Version: 0.1 (January, 2012)\n"
                                     "Copyright: All rights reserved to Leandro Cruz\n"
                                     "License: GPL\n\n"
                                     "Available on: www.impa.br/~lcruz/misc/games/snake"),
                                     this);
    labelTxt->setGeometry(20, 80, 360, 140);
    QPushButton* quitButtom = new QPushButton("Close", this);
    quitButtom->setGeometry(290, 235, 90, 30);
    connect(quitButtom, SIGNAL(pressed()), this, SLOT(ok()));
}

void AboutWindow::ok()
{
    control->tooglePause();
    close();
}

SaveWindow::SaveWindow(Control* c, QWidget *par)
    : control(c), WinWidget(par)
{
    resize(370, 240);
    setWindowTitle("Saving Game");
    QLabel* name = new QLabel("Name", this);
    name->setGeometry(25, 5, 120, 25);
    name->setAlignment(Qt::AlignHCenter);
    QLabel* level = new QLabel("Level", this);
    level->setGeometry(150, 5, 40, 25);
    level->setAlignment(Qt::AlignHCenter);
    QLabel* scores = new QLabel("Scores", this);
    scores->setGeometry(195, 5, 70, 25);
    scores->setAlignment(Qt::AlignHCenter);
    QLabel* date = new QLabel("Date", this);
    date->setGeometry(270, 5, 80, 25);
    date->setAlignment(Qt::AlignHCenter);
    QLabel* firstLabel = new QLabel("1-", this);
    firstLabel->setGeometry(5, 25, 15, 25);
    player1 = new QTextEdit(this);
    player1->setGeometry(20, 25, 125, 30);
    level1 = new QTextEdit(this);
    level1->setGeometry(150, 25, 40, 30);
    scores1 = new QTextEdit(this);
    scores1->setGeometry(195, 25, 70, 30);
    date1 = new QTextEdit(this);
    date1->setGeometry(270, 25, 95, 30);
    date1->setAlignment(Qt::AlignCenter);
    QLabel* secondLabel = new QLabel("2-", this);
    secondLabel->setGeometry(5, 60, 15, 25);
    player2 = new QTextEdit(this);
    player2->setGeometry(20, 60, 125, 30);
    level2 = new QTextEdit(this);
    level2->setGeometry(150, 60, 40, 30);
    scores2 = new QTextEdit(this);
    scores2->setGeometry(195, 60, 70, 30);
    date2 = new QTextEdit(this);
    date2->setGeometry(270, 60, 95, 30);
    date2->setAlignment(Qt::AlignCenter);
    QLabel* thirdLabel = new QLabel("3-", this);
    thirdLabel->setGeometry(5, 95, 15, 25);
    player3 = new QTextEdit(this);
    player3->setGeometry(20, 95, 125, 30);
    level3 = new QTextEdit(this);
    level3->setGeometry(150, 95, 40, 30);
    scores3 = new QTextEdit(this);
    scores3->setGeometry(195, 95, 70, 30);
    date3 = new QTextEdit(this);
    date3->setGeometry(270, 95, 95, 30);
    date3->setAlignment(Qt::AlignCenter);
    QLabel* fourthLabel = new QLabel("4-", this);
    fourthLabel->setGeometry(5, 130, 15, 25);
    player4 = new QTextEdit(this);
    player4->setGeometry(20, 130, 125, 30);
    level4 = new QTextEdit(this);
    level4->setGeometry(150, 130, 40, 30);
    scores4 = new QTextEdit(this);
    scores4->setGeometry(195, 130, 70, 30);
    date4 = new QTextEdit(this);
    date4->setGeometry(270, 130, 95, 30);
    date4->setAlignment(Qt::AlignCenter);
    QLabel* fifthLabel = new QLabel("5-", this);
    fifthLabel->setGeometry(5, 165, 15, 25);
    player5 = new QTextEdit(this);
    player5->setGeometry(20, 165, 125, 30);
    level5 = new QTextEdit(this);
    level5->setGeometry(150, 165, 40, 30);
    scores5 = new QTextEdit(this);
    scores5->setGeometry(195, 165, 70, 30);
    date5 = new QTextEdit(this);
    date5->setGeometry(270, 165, 95, 30);
    date5->setAlignment(Qt::AlignCenter);
    okButton = new QPushButton("OK", this);
    okButton->setGeometry(280, 205, 70, 30);
    okButton->setFocus();
    connect(okButton, SIGNAL(pressed()), this, SLOT(ok()));
}

void SaveWindow::setup(int l, int s, QString d)
{
    level = l;
    scores = s;
    date = d;
}

void SaveWindow::setGames(QVector<Game*> g)
{
    QString num;
    games = g;
    player1->setText( games[0]->getPlayer() );
    num.setNum( games[0]->getLevel() );
    level1->setText(num);
    num.clear();
    num.setNum( games[0]->getScores() );
    scores1->setText(num);
    date1->setText( games[0]->getDate().toString("dd/MM/yyyy") );
    player2->setText( games[1]->getPlayer() );
    num.setNum( games[1]->getLevel() );
    level2->setText(num);
    num.clear();
    num.setNum( games[1]->getScores() );
    scores2->setText(num);
    date2->setText( games[1]->getDate().toString("dd/MM/yyyy") );
    player3->setText( games[2]->getPlayer() );
    num.setNum( games[2]->getLevel() );
    level3->setText(num);
    num.clear();
    num.setNum( games[2]->getScores() );
    scores3->setText(num);
    date3->setText( games[2]->getDate().toString("dd/MM/yyyy") );
    player4->setText( games[3]->getPlayer() );
    num.setNum( games[3]->getLevel() );
    level4->setText(num);
    num.clear();
    num.setNum( games[3]->getScores() );
    scores4->setText(num);
    date4->setText( games[3]->getDate().toString("dd/MM/yyyy") );
    player5->setText( games[4]->getPlayer() );
    num.setNum( games[4]->getLevel() );
    level5->setText(num);
    num.clear();
    num.setNum( games[4]->getScores() );
    scores5->setText(num);
    date5->setText( games[4]->getDate().toString("dd/MM/yyyy") );
}

void SaveWindow::ok()
{
    QString player;
    int level, scores;
    QDate date;
    player = player1->toPlainText() == "" ? "Player 1" : player1->toPlainText();
    level = level1->toPlainText() == "" ? 1 : level1->toPlainText().toInt();
    scores = scores1->toPlainText() == "" ? 0 : scores1->toPlainText().toInt();
    date = date1->toPlainText() == "" ? QDate::currentDate() : QDate::fromString( date1->toPlainText(), "dd/MM/yyyy" );
    games[0]->setData(player, level, scores, date);
    player = player2->toPlainText() == "" ? "Player 1" : player2->toPlainText();
    level = level2->toPlainText() == "" ? 1 : level2->toPlainText().toInt();
    scores = scores2->toPlainText() == "" ? 0 : scores2->toPlainText().toInt();
    date = date2->toPlainText() == "" ? QDate::currentDate() : QDate::fromString( date2->toPlainText(), "dd/MM/yyyy" );
    games[1]->setData(player, level, scores, date);
    player = player3->toPlainText() == "" ? "Player 1" : player3->toPlainText();
    level = level3->toPlainText() == "" ? 1 : level3->toPlainText().toInt();
    scores = scores3->toPlainText() == "" ? 0 : scores3->toPlainText().toInt();
    date = date3->toPlainText() == "" ? QDate::currentDate() : QDate::fromString( date3->toPlainText(), "dd/MM/yyyy" );
    games[2]->setData(player, level, scores, date);
    player = player4->toPlainText() == "" ? "Player 1" : player4->toPlainText();
    level = level4->toPlainText() == "" ? 1 : level4->toPlainText().toInt();
    scores = scores4->toPlainText() == "" ? 0 : scores4->toPlainText().toInt();
    date = date4->toPlainText() == "" ? QDate::currentDate() : QDate::fromString( date4->toPlainText(), "dd/MM/yyyy" );
    games[3]->setData(player, level, scores, date);
    player = player5->toPlainText() == "" ? "Player 1" : player5->toPlainText();
    level = level5->toPlainText() == "" ? 1 : level5->toPlainText().toInt();
    scores = scores5->toPlainText() == "" ? 0 : scores5->toPlainText().toInt();
    date = date5->toPlainText() == "" ? QDate::currentDate() : QDate::fromString( date5->toPlainText(), "dd/MM/yyyy" );
    games[4]->setData(player, level, scores, date);
    close();
}
